﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This is a part of the core functionality of the server
 */

using MCServeLib.Types;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.IO;

namespace MCServeCore.Components
{
    public class ServerProperties
    {
        private DateTime _PropertiesTsCreate = new DateTime();
        public DateTime PropertiesTsCreate
        {
            set
            {
                _PropertiesTsCreate = value;
            }
            get
            {
                return _PropertiesTsCreate;
            }
        }

        private String _PropertiesVersion = "05-02-2013";
        public String PropertiesVersion
        {
            get
            {
                return _PropertiesVersion;
            }
        }

        private ObservableCollection<Property> _Properties = new ObservableCollection<Property>();
        public ObservableCollection<Property> Properties
        {
            get
            {
                return _Properties;
            }
        }

        public ServerProperties()
        {
            if (File.Exists(GameServer.Paths.Get(MCServePaths.SETTINGS)))
            {
                /*
                StreamReader ServerPropertiesFile = new StreamReader(MCServeCore.ServerData.ServerPropertiesPath);

                String ServerPropertiesContent = ServerPropertiesFile.ReadToEnd();
                String[] ServerPropertiesLines = ServerPropertiesContent.Split('\n');

                foreach (String ServerPropertiesLine in ServerPropertiesLines)
                {
                    if(ServerPropertiesLine[0] != '#')
                    {
                        String[] Property = ServerPropertiesLine.Split('=');
                        String Name = Property[0];
                        String Value = Property[1];

                        Add(new Property(Name, Value, Types.PropertyType.OBJECT));
                    }
                }

                ServerPropertiesFile.Close();
                 */
            }
            else
            {
                PropertiesTsCreate = DateTime.Now;
                Add(new Property("GeneratorSettings", "", PropertyType.STRING));
                Add(new Property("EnableCommandBlock", true, PropertyType.BOOLEAN));
                Add(new Property("AllowNether", true, PropertyType.BOOLEAN));
                Add(new Property("LevelName", "world", PropertyType.STRING));
                Add(new Property("AllowFlight", true, PropertyType.BOOLEAN));
                Add(new Property("ServerPort", 25565, PropertyType.INT));
                Add(new Property("LevelType", "DEFAULT", PropertyType.STRING));
                Add(new Property("LevelSeed", "", PropertyType.STRING));
                Add(new Property("ServerIp", "", PropertyType.STRING));
                Add(new Property("MaxBuildHeight", 256, PropertyType.INT));
                Add(new Property("SpawnNpcs", true, PropertyType.BOOLEAN));
                Add(new Property("WhiteList", true, PropertyType.BOOLEAN));
                Add(new Property("SpawnAnimals", true, PropertyType.BOOLEAN));
                Add(new Property("Hardcore", false, PropertyType.BOOLEAN));
                Add(new Property("TexturePack", "", PropertyType.STRING));
                Add(new Property("OnlineMode", true, PropertyType.BOOLEAN));
                Add(new Property("Pvp", false, PropertyType.BOOLEAN));
                Add(new Property("Difficulty", 2, PropertyType.INT));
                Add(new Property("Gamemode", "CREATIVE", PropertyType.STRING));
                Add(new Property("MaxPlayers", 20, PropertyType.INT));
                Add(new Property("SpawnMonsters", true, PropertyType.BOOLEAN));
                Add(new Property("GenerateStructures", true, PropertyType.BOOLEAN));
                Add(new Property("ViewDistance", 16, PropertyType.INT));
                Add(new Property("Motd", "Minecraft Server based on MCServe", PropertyType.STRING));
                Add(new Property("SpawnProtection", 16, PropertyType.INT));
                Add(new Property("SnooperEnabled", true, PropertyType.BOOLEAN));
                Add(new Property("EnableRcon", true, PropertyType.BOOLEAN));
                Add(new Property("EnableQuery", true, PropertyType.BOOLEAN));
                Add(new Property("EnableWebUi", true, PropertyType.BOOLEAN));
            }
        }

        public void Add(Property property)
        {
            _Properties.Add(property);
        }

        public void Remove(Guid guid)
        {
            for (int i = _Properties.Count - 1; i >= 0; i--)
            {
                if (_Properties[i].Guid == guid)
                {
                    _Properties.RemoveAt(i);
                }
            }
        }

        public Guid GetGuid(String title)
        {
            for (int i = _Properties.Count - 1; i >= 0; i--)
            {
                if (_Properties[i].Name == title)
                {
                    return _Properties[i].Guid;
                }
            }
            return new Guid();
        }

        public Object GetValue(String Object)
        {
            for (int i = _Properties.Count - 1; i >= 0; i--)
            {
                if (_Properties[i].Name == Object)
                {
                    return _Properties[i].Value;
                }
            }
            return null;
        }

        public void Change(Guid guid, String name, Object value)
        {
            StreamWriter ServerPropertiesFile = new StreamWriter(GameServer.Paths.Get(MCServeLib.Types.MCServePaths.SETTINGS));

            DateTime currentDate = DateTime.Now;

            ServerPropertiesFile.WriteLine("#Minecraft server properties by MCserve");
            ServerPropertiesFile.WriteLine("#" + currentDate.ToString("ddd MMM dd HH:mm:ss yyyy", CultureInfo.CreateSpecificCulture("en-GB")));

            for (int i = _Properties.Count - 1; i >= 0; i--)
            {
                if (_Properties[i].Guid == guid)
                {
                    _Properties[i].Name = name;
                    _Properties[i].Value = value;
                }
            }

            foreach (Property PropertyEntry in GameServer.ServerProperties.Properties)
            {
                ServerPropertiesFile.WriteLine(PropertyEntry.Name + "=" + PropertyEntry.Value);
            }

            ServerPropertiesFile.Close();
        }
    }
}